package com.hddz.acquisition.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hddz.acquisition.R;
import com.hddz.acquisition.model.DetailViewItem;

public class DetailAdapter extends BaseAdapter {
	private List<DetailViewItem> viewItems;// 定义一个list对象
	private Context mContext;// 上下文

	public static Object[][] getDefaultValue(Object[][] titles) {
		Object[][] result = new Object[titles.length][];
		for (int i = 0; i < result.length; i++) {
			result[i] = new Object[titles[i].length];
			for (int j = 0; j < result[i].length; j++) {
				result[i][j] = null;
			}
		}
		return result;
	}
	

	/**
	 * 构造方法
	 * 
	 * @param context
	 *            上下文
	 * @param titles
	 *            所有APP的集合
	 * @param page
	 *            当前页
	 */
	public DetailAdapter(Context context, Integer[][] titles, Object[][] values, String[][] formats) {
		mContext = context;
		viewItems = new ArrayList<DetailViewItem>();
		for (int i = 0; i < titles.length; i++) {
			DetailViewItem item = new DetailViewItem();
			for (int j = 0; j < titles[i].length; j++) {
				if (titles[i][j] > 0)
					item.appand(titles[i][j], values[i][j], formats[i][j]);
			}
			viewItems.add(item);
		}
	}

	public DetailAdapter(Context context, String[][] titles, Object[][] values, String[][] formats) {
		mContext = context;
		viewItems = new ArrayList<DetailViewItem>();
		for (int i = 0; i < titles.length; i++) {
			DetailViewItem item = new DetailViewItem();
			for (int j = 0; j < titles[i].length; j++) {
				if (titles[i][j] != null && !titles[i][j].equals(""))
					item.appand(titles[i][j], values[i][j], formats[i][j]);
			}
			viewItems.add(item);
		}
	}

	public void setValue(Integer titleRes, Object value) {
		for (DetailViewItem item : viewItems) {
			item.update(titleRes, value);
		}
	}

	public Object getValue(Integer titleRes) {
		for (DetailViewItem item : viewItems) {
			if (item.exist(titleRes)) {
				return item.get(titleRes);
			}
		}
		return null;
	}
	
	public void setValue(String titleStr, Object value) {
		for (DetailViewItem item : viewItems) {
			item.update(titleStr, value);
		}
	}

	public Object getValue(String titleStr) {
		for (DetailViewItem item : viewItems) {
			if (item.exist(titleStr)) {
				return item.get(titleStr);
			}
		}
		return null;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return viewItems.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return viewItems.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		DetailViewItem item = viewItems.get(position);
		if (item.getCount() == 1) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.view_xx_line_type_one_item, parent, false);
		} else {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.view_xx_line_type_two_item, parent, false);
		}
		if (item.getCount() == 1) {
			((TextView) convertView.findViewById(R.id.txt_title)).setText(mContext.getResources().getString(item.getTitle()));
			((TextView) convertView.findViewById(R.id.txt_value)).setText(item.getValue());
		} else {
			((TextView) convertView.findViewById(R.id.txt_title)).setText(mContext.getResources().getString(item.getTitle()));
			((TextView) convertView.findViewById(R.id.txt_value)).setText(item.getValue());
			((TextView) convertView.findViewById(R.id.txt_title_1)).setText(mContext.getResources().getString(item.getTitle1()));
			((TextView) convertView.findViewById(R.id.txt_value_1)).setText(item.getValue1());
		}
		return convertView;
	}
}
